const state ={
    visitedViews:[]
}
const mutations={
    //添加点击的路由到visitedViews
    ADD_VISITED_VIEWS(state,view){
        if (state.visitedViews.some(v => v.path === view.path)) return
        console.log(view)
        state.visitedViews.push(
            Object.assign({},view,{
                title: view.meta.title || 'no-name'
            })
        )
    },
    //删除visitedViews中的选中路由
    DEL_VISITED_VIEWS(state,view){
        for (const [i,v] of state.visitedViews.entries()){
            if(v.path === view.path){
                state.visitedViews.splice(i,1)
                break;
            }
        }
    },
    //删除visitedViews中的所有路由，除了meta.affix设置为ture
    DEL_ALL_VISITED_VIEWS(state){
        const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
        state.visitedViews = affixTags
    }

}

const actions={
    addVisitedViews({ commit },view){
        commit('ADD_VISITED_VIEWS',view)
    },
    delVisitedView({ commit, state },view){
        return new Promise(resolve => {
            commit('DEL_VISITED_VIEWS',view)
            resolve([...state.visitedViews])
        })
    },
    delAllVisitedView({commit, state }){
        return new Promise(resolve => {
            commit('DEL_ALL_VISITED_VIEWS')
            resolve({
                visitedViews: [...state.visitedViews],
            })
        })
    }
}

export default {
    namespaced:true,
    state,
    mutations,
    actions
}
